import java.lang.*;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
 * This is the MapController where all the controls for the map are.
 * @author paulsmith
 *
 */
public class MapController {
        
        private StatusModel sm;
        private MilestoneModel mm;
        private InventoryModel im;
        private StoreView stv;
        
        private double playerX, playerY;
        private int nextIndex;
        
        private RiverCrossingView rcv;
        
        /**
         * This is the mapController constructor that initializes the parameters
         * @param sm            sm is StatusModel
         * @param mm            mm is MilestoneModel
         */
        public MapController(StatusModel sm, MilestoneModel mm, InventoryModel im, StoreView stv) {
                this.sm = sm;
                this.mm = mm;
                this.im = im;
                this.stv = stv;
                rcv = new RiverCrossingView(im);
                
                playerX = 702;
                playerY = 494;
                nextIndex = 1;
        }
        /**
         * This method is where players movements are controlled
         * @param distance              
         */
        public void movePlayer(double distance){
        double distanceToMilestone = Math.sqrt(Math.pow(mm.getSpotX(nextIndex)-playerX, 2) + Math.pow(mm.getSpotY(nextIndex)-playerY, 2));
        if(distance>distanceToMilestone){
                distance = distanceToMilestone;
        }
        double distanceTraveled = 0;
        double pastX = playerX;
        double pastY = playerY;
        while(distanceTraveled<=distance){
                playerX += mm.getMovementX(nextIndex);
                playerY += mm.getMovementY(nextIndex);
                distanceTraveled = Math.sqrt(Math.pow(playerX-pastX, 2)+Math.pow(playerY-pastY, 2));            
        }
        
        double newDistance = sm.getDistTravelled() + distance;
        sm.setDistTravelled((int)newDistance);
        
        if(playerX<mm.getSpotX(nextIndex)){
                playerX = mm.getSpotX(nextIndex);
                playerY = mm.getSpotY(nextIndex);
                runMilestone();
                nextIndex +=1;
        }
        
    }
        /**
         * This method calls the particular milestone and runs it
         */
        private void runMilestone() {
                if(mm.getType(nextIndex) == 0) {
                        /*JOptionPane.showMessageDialog(new JFrame("Congrats!"),
                                                "You are approaching " + mm.getName(nextIndex));*/
                        if (mm.getStore(nextIndex)) {
                                stv.setVisible(true);
                        }
                } else if (mm.getType(nextIndex) == 1) {
                        rcv.setVisible(true);
                }
        }
        /**
         * This is the getter for the playerX
         * @return playerX
         */
        public double getPlayerX() {
                return playerX;
        }
        /**
         * This is the getter for the playerY
         * @return playerY
         */
        public double getPlayerY() {
                return playerY;
        }
        /**
         * This is the getter for the nextIndex
         * @return nextIndex            nextIndex is the next milestone index
         */
        public int getNextIndex() {
                return nextIndex;
        }
        /**
         * This is the setter for the nextIndex
         * @param nextIndex                     nextIndex is the next milestone index
         */
        public void setNextIndex(int nextIndex) {
                this.nextIndex = nextIndex;
        }
        
        public void setPlayerX(double playerX)
        {
        	this.playerX = playerX;
        }
        
        public void setPlayerY(double playerY)
        {
        	this.playerY  = playerY;
        }
}